home *** CD-ROM | disk | FTP | other *** search
/ Gekikoh Dennoh Club 5 / Gekikoh Dennoh Club Vol. 5 (Japan).7z / Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin / internet / webx / rbget020.lzh / GetFile / GetFile.c
Encoding:
C/C++ Source or Header  |  1998-09-27  |  11.7 KB  |  470 lines

  1. /* GetFile.c */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/dos.h>
  7. #include <sys/iocs.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <alloca.h>
  11.  
  12. #include <network.h>
  13. #include <socket.h>
  14.  
  15. #ifdef    WEBXPRESSION
  16. #include "../WebXpression.h"
  17. extern void show_mouse_cursor (void);
  18. extern void hide_mouse_cursor (void);
  19. extern unsigned char hold_online;
  20. unsigned char from_network;
  21. #else
  22. #include "../RoboGet.h"
  23. extern char QuitCheck (void);
  24. #endif
  25.  
  26. #include "../WebCache/WebCache.h"
  27. #include "../FuncUrl/FuncUrl.h"
  28.  
  29. extern void Date2Date (char *, struct tm *);
  30. extern void McPrint (char *);
  31. extern void McDbPrint (char *);
  32. extern void McCursorTop (void);
  33. extern volatile signed char quit_flag;
  34.  
  35. static int inetd_version;
  36.  
  37. static char *content_type_str[]=
  38. {"text/html", "text/html", "text/html", "text/plain", "text/plain",
  39.  "image/gif", "image/jpeg", "image/jpeg", "image/ping", "image/bmp",
  40.  "application/zip", "application/lzh", "application/gzip",
  41.  "application/pdf",
  42.  "application/octet-stream", "text/plain"};
  43. static char *ext_type_str[]=
  44. {"HTM", "HTML", "CGI", "TXT", "DOC",
  45.  "GIF", "JPG", "JPEG", "PNG", "BMP",
  46.  "ZIP", "LZH", "TGZ",
  47.  "PDF",
  48.  "DAT", "*/*"};
  49.  
  50. enum {
  51.     REQ_HEAD,
  52.     REQ_GET
  53. };
  54.  
  55.  
  56. int GetFileInit (void)
  57. {
  58.     if ((inetd_version = _get_version ())< 0) {
  59.         printf ("ìÉÆm : TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóé▄é╣é±üB\n"
  60.             "    âìü[âJâïâtâ@âCâïü^WebCache âtâ@âCâïé╠é▌ë{ùùé¬ë┬ö\é┼é╖üB\n");
  61.     }
  62.     return (0);
  63. }
  64.  
  65.  
  66. void HideMouse (void)
  67. {
  68. #ifdef    WEBXPRESSION
  69.     int sp;
  70.  
  71.     from_network = !0;
  72.     if (hold_online) {
  73.         sp = _iocs_b_super (0);
  74.         hide_mouse_cursor ();
  75.         _iocs_b_super (sp);
  76.     }
  77. #endif
  78. }
  79.  
  80. void ShowMouse (void)
  81. {
  82. #ifdef    WEBXPRESSION
  83.     int sp;
  84.  
  85.     from_network = 0;
  86.     if (hold_online) {
  87.         sp = _iocs_b_super (0);
  88.         show_mouse_cursor ();
  89.         _iocs_b_super (sp);
  90.     }
  91. #endif
  92. }
  93.  
  94.  
  95.  
  96. /* URL é┼ÄwÆΦé╡é╜âtâ@âCâïé≡âlâbâgâÅü[âNé⌐éτĵé┴é─é¡éΘ */
  97. static signed char GetFromNetwork (HTTPFILE * httpfile, char req_mode)
  98. {
  99.     int netd;
  100.     struct sockaddr_in addr;
  101.     struct hostent *h;
  102.     char scheme[256], hostname[256], path[256], fname[256], query[256], anchor[256];
  103.     int port;
  104.     char temp_str[1024];
  105.  
  106.     httpfile->content_length = 0;
  107.     *httpfile->content_type = '\0';
  108.     httpfile->content = NULL;
  109.  
  110. #define NO_TIMESTAMP    255
  111.     (httpfile->time_stamp).tm_sec = NO_TIMESTAMP;
  112.  
  113.     UrlSplit (httpfile->url, scheme, hostname, path, fname, query, anchor, &port);
  114.  
  115.     /* â\âPâbâgé≡ì∞ɼé╖éΘ */
  116.     if ((netd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
  117.         McPrint ("üª â\âPâbâgé¬ì∞ɼé┼é½é▄é╣é±é┼é╡é╜\n");
  118.         return (-1);
  119.     }
  120.     memset (&addr, 0, sizeof (addr));    /* 0 é┼ûäé▀éΘ */
  121.  
  122.     addr.sin_family = AF_INET;    /* INETâhâüâCâôé≡ÄwÆΦ */
  123.     addr.sin_port = htons (port);    /* http é═â|ü[âg 80ö╘ */
  124.     /* âzâXâgû╝(www.xxx.co.jp) é≡ IP âAâhâîâX(int)é╔ò╧è╖ */
  125.     if ((h = gethostbyname (hostname)) == NULL) {
  126.         McPrint ("üªâhâüâCâôû╝é¬é▌é┬é⌐éΦé▄é╣é±\n");
  127.         return (-1);
  128.     }
  129.     addr.sin_addr.s_addr = *(long *) h->h_addr;
  130.  
  131.     /* æèÄΦɵé╔É┌æ▒é╖éΘ */
  132.     if (req_mode == REQ_HEAD)
  133.         McDbPrint ("âwâbâ_é≡ĵô╛é╡é▄é╖");
  134.     McPrint (hostname);
  135.     McPrint (" é╔É┌æ▒Æå...");
  136.     if (connect (netd, (char *) &addr, sizeof (addr)) < 0) {
  137.         McPrint ("\nüª É┌æ▒é╔Ä╕ösé╡é▄é╡é╜\n");
  138.         return (-1);
  139.     }
  140.     McCursorTop ();
  141.     McPrint (hostname);
  142.     McPrint (" é╔É┌æ▒é╡é▄é╡é╜\n");
  143.  
  144.     if (req_mode == REQ_HEAD)
  145.         sprintf (temp_str, "HEAD %s%s%s HTTP/1.0\r\n", path, fname, query);
  146.     else
  147.         sprintf (temp_str, "GET %s%s%s HTTP/1.0\r\n", path, fname, query);
  148.     McDbPrint (temp_str);
  149.     write_s (netd, temp_str, strlen (temp_str));
  150.     /* write_s (netd, "User-Agent: WebXpression / ver0.01 (X68000)\r\n", 48);     */
  151.     strcpy (temp_str, "Accept: */*\r\n");
  152.     write_s (netd, temp_str, strlen (temp_str));
  153.     sprintf (temp_str, "Host: %s\r\n", hostname);
  154.     write_s (netd, temp_str, strlen (temp_str));
  155.     write_s (netd, "\r\n", 2);
  156.  
  157.     McPrint ("âîâXâ|âôâXé≡æ╥é┐é▄é╖...");
  158.  
  159.     /* âwâbâ_é≡éPìsé├é┬ô╟é▌ì₧é▐ */
  160.     recvline (netd, temp_str, 1024);
  161.     if ((strncmp (temp_str, "HTTP/1.0 200", 12))
  162.         && (strncmp (temp_str, "HTTP/1.1 200", 12))) {
  163.         McDbPrint (temp_str);
  164.         close_s (netd);    /* É┌æ▒é╠É╪Æf */
  165.         return (-1);
  166.     }
  167.     McCursorTop ();        /* "âîâXâ|âôâXé≡æ╥é┐é▄é╖"é≡Å┴ïÄ */
  168.  
  169.     /* ï≤ìsüiâwâbâ_é╠ÅIù╣üjé¬ùêéΘé▄é┼âïü[âv */
  170.     while (recvline (netd, temp_str, 1024), strlen (temp_str) > 3) {
  171.         char temp_entity[256];
  172.  
  173.         McDbPrint ("HEAD > ");
  174.         McDbPrint (temp_str);
  175.  
  176.         sscanf (temp_str, "%s", temp_entity);
  177.         /* âwâbâ_û╝é═æσò╢ÄÜü^żò╢ÄÜé≡ïµò╩é╡é╚éó */
  178.         if (!stricmp (temp_entity, "Content-Type:")) {
  179.             sscanf (temp_str + 14, "%s", httpfile->content_type);
  180.         }
  181.         if (!stricmp (temp_entity, "Content-Length:")) {
  182.             sscanf (temp_str + 16, "%d", &httpfile->content_length);
  183.         }
  184.         if (!stricmp (temp_entity, "Last-Modified:")) {
  185.             Date2Date (temp_str + 15, &(httpfile->time_stamp));
  186.         }
  187.     }
  188.     if (*httpfile->content_type == '\0')
  189.         strcpy (httpfile->content_type, "application/octet-stream");
  190.  
  191.  
  192.     if (req_mode == REQ_HEAD) {
  193.         close_s (netd);    /* É┌æ▒é╠É╪Æf */
  194.  
  195.         if ((httpfile->time_stamp).tm_sec == NO_TIMESTAMP) {
  196.             time_t t;
  197.             t = time (NULL);
  198.             httpfile->time_stamp = *localtime (&t);
  199.         }
  200.         McCursorTop ();
  201.         McPrint ("âwâbâ_é≡ĵô╛é╡é▄é╡é╜\n");
  202.     } else {
  203.         FILE *fp;
  204.         int read_size = 0, s;
  205.         void *r = NULL;
  206.         int alloc_size, alloc_left;
  207.         int lap_time;
  208.         char cache_fname[256];
  209.  
  210. #define YOYUU    4096
  211.         if (httpfile->content_length) {
  212.             alloc_size = httpfile->content_length + YOYUU;
  213.         } else {
  214.             /* 12MB ê╚Åπèmò█é┼é½é╚éóéµü[é± */
  215.             alloc_size = ((int) _dos_malloc (-1)) & 0x00fffffe - YOYUU;
  216.         }
  217.         if (((int) (httpfile->content = _dos_malloc (alloc_size))) < 0) {
  218.             McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
  219.             close_s (netd);    /* É┌æ▒é╠É╪Æf */
  220.             httpfile->content = NULL;
  221.             return (-1);
  222.         }
  223.         r = httpfile->content;
  224.         alloc_left = alloc_size;
  225.         lap_time = _iocs_ontime ();
  226.         quit_flag = QUIT_NON;
  227.         while ((socklen (netd, 0) >= 0) && (quit_flag == QUIT_NON)) {
  228.             s = read_s (netd, r, alloc_left);
  229.             read_size += s;
  230.             r += s;
  231.             if ((alloc_left -= s) <= 0) {
  232.                 McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
  233.                 break;
  234.             }
  235. #if    1
  236.             if (httpfile->content_length)
  237.                 sprintf (temp_str, "Ä≤ÉMÆå %d/%d âoâCâg", read_size, httpfile->content_length);
  238.             else
  239.                 sprintf (temp_str, "Ä≤ÉMÆå %d âoâCâg", read_size);
  240.             McCursorTop ();    /* âJü[â\âïé≡ìsé╠ɵô¬é╔ */
  241.             McPrint (temp_str);
  242. #endif
  243. #ifndef    WEBXPRESSION
  244.             quit_flag = QuitCheck ();
  245. #endif
  246.         }
  247.         if (quit_flag != QUIT_NON){
  248.             shutdown(netd,0); /* Ä≤ÉMé╡é╜âfü[â^é≡Ä≤é»Äµéτé╕üCé╖é╫é─öpèⁿé╖éΘ */
  249.             shutdown(netd,2); /* connection é≡ aborté╖éΘ */
  250.         }
  251.         close_s (netd);    /* É┌æ▒é╠É╪Æf */
  252.         lap_time -= _iocs_ontime ();
  253.         McCursorTop ();    /* âJü[â\âïé≡ìsé╠ɵô¬é╔ */
  254.  
  255. #ifndef WEBXPRESSION
  256.         sprintf (temp_str, "    Ä≤ÉMÅIù╣üiÄ≤ÉMæ¼ôxé═ %8.1f âoâCâgü^òbé┼é╡é╜üj", (double) read_size / (double) (-lap_time) * 100.0);
  257.         McPrint (temp_str);
  258. #endif
  259.  
  260.         if (!(httpfile->content_length)) {
  261.             _dos_setblock (httpfile->content, read_size);
  262.             httpfile->content_length = read_size;
  263.             if (alloc_left <= 0) {
  264.                 _dos_mfree (httpfile->content);
  265.                 httpfile->content = NULL;
  266.                 return (-1);
  267.             }
  268.         }
  269.         if (quit_flag == QUIT_NON) {
  270.             time_t t;
  271.             if ((httpfile->time_stamp).tm_sec != NO_TIMESTAMP) {
  272.                 t = mktime (&httpfile->time_stamp);
  273.             } else {
  274.                 t = time (NULL);
  275.                 httpfile->time_stamp = *localtime (&t);
  276.             }
  277.             WCInsertUrl (httpfile, cache_fname);
  278.             if ((fp = fopen (cache_fname, "wb")) != NULL) {
  279.                 struct utimbuf u;
  280.                 fwrite (httpfile->content, sizeof (char), read_size, fp);
  281.                 fclose (fp);
  282.                 u.actime = t + 32400;
  283.                 u.modtime = t + 32400;
  284.                 utime (cache_fname, &u);
  285. #ifndef WEBXPRESSION
  286.                 McPrint (" : âtâ@âCâïé≡ò█æ╢é╡é▄é╡é╜\n");
  287. #endif
  288.             }
  289.         } else {
  290.             McPrint ("üª ÆåÆfé╡é▄é╡é╜\n");
  291.             _dos_mfree (httpfile->content);
  292.             httpfile->content = NULL;
  293.         }
  294.     }
  295.     return (0);
  296. }
  297.  
  298.  
  299.  
  300. /* URL é┼ÄwÆΦé╡é╜âtâ@âCâïé≡âfâBâXâNé⌐éτĵé┴é─é¡éΘ */
  301. static void GetFromLocal (HTTPFILE * httpfile)
  302. {
  303.     FILE *fp;
  304.     char scheme[256], hostname[256], path[256], fname[256], query[256], anchor[256];
  305.     int port;
  306.     char temp_str[256], ext[256];
  307.  
  308.     *httpfile->content_type = '\0';
  309.     httpfile->content_length = 0;
  310.  
  311.     UrlSplit (httpfile->url, scheme, hostname, path, fname, query, anchor, &port);
  312.     strcpy (temp_str, hostname);
  313.     strcat (temp_str, path);
  314.     strcat (temp_str, fname);
  315.     if ((fp = fopen (temp_str, "rb")) != NULL) {
  316.         int i;
  317.         char c;
  318.         char *p;
  319.  
  320.         httpfile->content_length = filelength (fileno (fp));
  321.         if ((int) (httpfile->content = _dos_malloc (httpfile->content_length)) > 0) {
  322.             fread (httpfile->content, httpfile->content_length, 1, fp);
  323.             fclose (fp);
  324.  
  325.             /* â}âïâ`âsâèâIâhö±æ╬ë₧üEüEüE */
  326.             ext[0] = '\0';
  327.             p = &fname[0];
  328.             while (c = *p++) {
  329.                 if (c == '.') {
  330.                     strcpy (ext, p);
  331.                     break;
  332.                 }
  333.             }
  334.  
  335.             for (i = 0; i < sizeof (ext_type_str) / sizeof (char *)- 1; i++) {
  336.                 if (!stricmp (ext, ext_type_str[i]))
  337.                     break;
  338.             }
  339.             strcpy (httpfile->content_type, content_type_str[i]);
  340.             /* éáüAâ^âCâÇâXâ^âôâvé╠Åêù¥é▄é╛ */
  341.  
  342.         } else {
  343.             McDbPrint ("GetFromLocal() : ");
  344.             McPrint ("üª : âüâéâèé¬æ½éΦé▄é╣é±\n");
  345.             httpfile->content = NULL;
  346.         }
  347.     } else {
  348.         McDbPrint ("GetFromLocal() : ");
  349.         McPrint ("üª : âìü[âJâïâtâ@âCâï \n");
  350.         McPrint (httpfile->url);
  351.         McPrint (" é¬âIü[âvâôé┼é½é▄é╣é±\n");
  352.     }
  353.     return;
  354. }
  355.  
  356.  
  357.  
  358. /* URL é┼ÄwÆΦé╡é╜âtâ@âCâïé≡âLâââbâVâàé⌐éτĵé┴é─é¡éΘ */
  359. static void GetFromCache (HTTPFILE * httpfile, char *cache_fname)
  360. {
  361.     FILE *fp;
  362.  
  363. #ifndef WEBXPRESSION
  364.     if (!strcmp (httpfile->content_type, "text/html"))
  365.         return;
  366. #endif
  367.     McDbPrint ("âLâââbâVâàé⌐éτ ");
  368.     McDbPrint (cache_fname);
  369.     McDbPrint (" é≡ô╟é▌ì₧é▌é▄é╖\n");
  370.  
  371.     if ((fp = fopen (cache_fname, "rb")) != NULL) {
  372.         if ((int) (httpfile->content = _dos_malloc (httpfile->content_length)) > 0) {
  373.             fread (httpfile->content, httpfile->content_length, sizeof (char), fp);
  374.         } else {
  375.             McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
  376.             httpfile->content = NULL;
  377.         }
  378.         fclose (fp);
  379.     } else {
  380.         McPrint ("üª âLâââbâVâàâtâ@âCâï ");
  381.         McPrint (cache_fname);
  382.         McPrint (" é¬âIü[âvâôé┼é½é▄é╣é±\n");
  383.     }
  384.     return;
  385. }
  386.  
  387.  
  388.  
  389. /* URL é┼ÄwÆΦé╡é╜âtâ@âCâïé≡ĵé┴é─é¡éΘ */
  390. int GetFile (HTTPFILE * httpfile)
  391. {
  392.     char cache_fname[256];
  393.     char head_flag;
  394.  
  395.     McDbPrint ("GetFile() : ");
  396.     McDbPrint (httpfile->url);
  397.     McDbPrint (" é╠Ä≤ÉMé≡èJÄné╡é▄é╖");
  398.  
  399.     httpfile->content = NULL;
  400.  
  401.     switch (WCExist (httpfile, cache_fname)) {
  402.     case WC_NON:        /* âLâââbâVâàé╔æ╢ì▌é╡é╚éóÅΩìç */
  403.         McDbPrint (" / WC_NON\n");
  404.         if (inetd_version >= 0) {
  405.             /* TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóéΘÅΩìç */
  406.             HideMouse ();
  407.             GetFromNetwork (httpfile, REQ_GET);
  408.             ShowMouse ();
  409.         } else {
  410.             McPrint ("üª TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóé▄é╣é±\n");
  411.         }
  412.         break;
  413.  
  414.     case WC_INCACHE:    /* âLâââbâVâàé╔æ╢ì▌é╡üAïNô«îπÅëé▀é─é╠âAâNâZâXé╠ÅΩìç */
  415.         McDbPrint (" / WC_INCACHE\n");
  416.         WCSetAccess (httpfile);
  417.         if (inetd_version >= 0) {
  418.             /* TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóéΘÅΩìç */
  419.             HTTPFILE *httpfile2;    /* âwâbâ_é¬ò╘éΘ */
  420.  
  421.             httpfile2 = alloca (sizeof (HTTPFILE));
  422.             strcpy (httpfile2->url, httpfile->url);
  423.             HideMouse ();
  424.             head_flag = GetFromNetwork (httpfile2, REQ_HEAD);    /* âwâbâ_é≡ĵô╛ */
  425.             ShowMouse ();
  426.             if (!head_flag) {
  427.                 /* âwâbâ_é¬Äµô╛Åoùêé╜ÅΩìç */
  428.                 McDbPrint ("âwâbâ_ĵô╛ɼî≈\n");
  429.                 if (difftime (mktime (&httpfile2->time_stamp), mktime (&httpfile->time_stamp)) > (double) 0.0) {
  430.                     /* âtâ@âCâïé¬ìXÉVé│éΩé─éóé╜ÅΩìç */
  431.                     McDbPrint ("âtâ@âCâïé¬ìXÉVé│éΩé─éóé▄é╖\n");
  432.                     HideMouse ();
  433.                     GetFromNetwork (httpfile, REQ_GET);
  434.                     ShowMouse ();
  435.                     if (httpfile->content != NULL) {
  436.                         WCDeleteUrl (httpfile->url);
  437.                         remove (cache_fname);
  438.                     }
  439.                 } else {
  440.                     /* âtâ@âCâïé¬ìXÉVé│éΩé─éóé╚éóÅΩìç */
  441.                     McDbPrint ("âtâ@âCâïé¬ìXÉVé│éΩé─éóé▄é╣é±\n");
  442.                     GetFromCache (httpfile, cache_fname);
  443.                 }
  444.             } else {
  445.                 /* âwâbâ_é¬Äµô╛é┼é½é╚é⌐é┴é╜ÅΩìç */
  446.                 GetFromCache (httpfile, cache_fname);
  447.             }
  448.         } else {
  449.             /* TCP/IP âhâëâCâoé¬ÅφÆôé╡é─éóé╚éóÅΩìç */
  450.             GetFromCache (httpfile, cache_fname);
  451.         }
  452.         break;
  453.  
  454.     case WC_INCACHE2:    /* âLâââbâVâàé╔æ╢ì▌é╡üAïNô«îπéPë±ê╚ÅπâAâNâZâXé╡é─éóéΘÅΩìç */
  455.         McDbPrint (" / WC_INCACHE2\n");
  456.         GetFromCache (httpfile, cache_fname);
  457.         break;
  458.  
  459.     case WC_LOCAL:
  460.         McDbPrint (" / WC_LOCAL\n");
  461.         GetFromLocal (httpfile);
  462.         break;
  463.     }
  464.  
  465.     if (httpfile->content == NULL)
  466.         return (-1);
  467.  
  468.     return (0);
  469. }
  470.